package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.math.Vector2;
import com.corona.Game;
import com.corona.game.util.Save;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/* compiled from: PkmnMap.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/GenerateWorld.class */
public class GenerateWorld extends Action {
    public int size;
    public String enterDirection;
    public float percent = 1.0f;
    public Action.Layer layer = Action.Layer.map_0;

    public GenerateWorld(int size, String enterDirection, Action nextAction) {
        super(new Object[0]);
        this.size = size;
        this.enterDirection = enterDirection;
        this.nextAction = nextAction;
    }

    @Override // com.corona.game.Action
    public Action.Layer getLayer() {
        return this.layer;
    }

    @Override // com.corona.game.Action
    public void firstStep(final Game game) {
        game.actionStack.remove(this);
        DrawMiniMap.warpTiles.clear();
        Iterator it = new ArrayList(game.actionStack).iterator();
        while (it.hasNext()) {
            Action action = (Action) it.next();
            if (action instanceof Pokemon.Emote) {
                game.actionStack.remove(action);
            } else if (action instanceof CycleDayNight) {
                game.actionStack.remove(action);
            }
        }
        for (Pokemon pokemon : game.map.pokemon.values()) {
            pokemon.removeDrawActions(game);
        }
        game.actionStack.remove(game.map.upkeepTimers);
        System.out.println("Action stack before loading new area");
        System.out.println("Layer, Name");
        Iterator<Action> it2 = game.actionStack.iterator();
        while (it2.hasNext()) {
            Action action2 = it2.next();
            System.out.println(String.valueOf(action2.getLayer()) + "  " + action2.getClass().getName());
        }
        String[] vals = game.map.currMapId.split(",");
        int xPos = Integer.valueOf(vals[0]).intValue();
        int yPos = Integer.valueOf(vals[1]).intValue();
        if (this.enterDirection.equals("left")) {
            this.percent = (game.player.position.y - game.map.bottomLeft.y) / (game.map.topRight.y - game.map.bottomLeft.y);
            xPos--;
        } else if (this.enterDirection.equals("right")) {
            this.percent = (game.player.position.y - game.map.bottomLeft.y) / (game.map.topRight.y - game.map.bottomLeft.y);
            xPos++;
        } else if (this.enterDirection.equals("up")) {
            this.percent = (game.player.position.x - game.map.bottomLeft.x) / (game.map.topRight.x - game.map.bottomLeft.x);
            yPos++;
        } else if (this.enterDirection.equals("down")) {
            this.percent = (game.player.position.x - game.map.bottomLeft.x) / (game.map.topRight.x - game.map.bottomLeft.x);
            yPos--;
        }
        final String adjacentWorldId = xPos + "," + yPos;
        if (Save.isDataPresent(game.map.id + ".sav/map" + adjacentWorldId)) {
            System.out.println("Found existing world:");
            System.out.println("map" + adjacentWorldId);
            game.map.saveToFileNew(game);
            game.map.overworldTiles.clear();
            game.map.pokemon.clear();
            game.map.edges.clear();
            Iterator<HashMap<Vector2, Tile>> it3 = game.map.interiorTiles.iterator();
            while (it3.hasNext()) {
                HashMap<Vector2, Tile> tiles = it3.next();
                if (tiles != null) {
                    tiles.clear();
                }
            }
            game.map.interiorTiles.clear();
            game.map.minimap.dispose();
            String prevTimeOfDay = game.map.timeOfDay;
            game.map = new PkmnMap(game.map.id);
            game.map.timeOfDay = prevTimeOfDay;
            game.map.currMapId = adjacentWorldId;
            game.insertAction(game.map.upkeepTimers);
            game.map.loadMapFromFile(game);
            InputProcessor.aJustPressed = false;
            Vector2 startLoc = new Vector2();
            if (this.enterDirection.equals("left")) {
                startLoc.x = game.map.topRight.x;
                startLoc.y = game.map.bottomLeft.y + (this.percent * (game.map.topRight.y - game.map.bottomLeft.y));
            } else if (this.enterDirection.equals("right")) {
                startLoc.x = game.map.bottomLeft.x;
                startLoc.y = game.map.bottomLeft.y + (this.percent * (game.map.topRight.y - game.map.bottomLeft.y));
            } else if (this.enterDirection.equals("up")) {
                startLoc.y = game.map.bottomLeft.y;
                startLoc.x = game.map.bottomLeft.x + (this.percent * (game.map.topRight.x - game.map.bottomLeft.x));
            } else if (this.enterDirection.equals("down")) {
                startLoc.y = game.map.topRight.y;
                startLoc.x = game.map.bottomLeft.x + (this.percent * (game.map.topRight.x - game.map.bottomLeft.x));
            }
            startLoc.x -= startLoc.x % 16.0f;
            startLoc.y -= startLoc.y % 16.0f;
            game.player.position.set(startLoc);
            game.cam.position.set(startLoc.x + 16.0f, startLoc.y, 0.0f);
            EnterBuilding enterBuilding = new EnterBuilding(game, "", null);
            enterBuilding.slow = 8;
            game.insertAction(enterBuilding);
            game.insertAction(this.nextAction);
            game.insertAction(new CycleDayNight(game));
            game.musicController.startTimeOfDay = game.map.timeOfDay;
            return;
        }
        final Action drawControls = new DrawControls();
        game.map.saveToFileNew(game);
        game.map.overworldTiles.clear();
        game.map.pokemon.clear();
        game.map.edges.clear();
        Iterator<HashMap<Vector2, Tile>> it4 = game.map.interiorTiles.iterator();
        while (it4.hasNext()) {
            HashMap<Vector2, Tile> tiles2 = it4.next();
            if (tiles2 != null) {
                tiles2.clear();
            }
        }
        game.map.interiorTiles.clear();
        game.map.minimap.dispose();
        String prevTimeOfDay2 = game.map.timeOfDay;
        game.map = new PkmnMap(game.map.id);
        game.map.timeOfDay = prevTimeOfDay2;
        final int size = this.size;
        final String enterDirection = this.enterDirection;
        final float percent = this.percent;
        final Action nextAction = this.nextAction;
        Thread thread = new Thread(new Runnable() { // from class: com.corona.game.GenerateWorld.1
            @Override // java.lang.Runnable
            public void run() {
                try {
                    System.out.println("Generating map...");
                    System.out.println(LocalTime.now());
                    final GenIsland1 genIsland = new GenIsland1(game, new Vector2(0.0f, 0.0f), size);
                    System.out.println("Done.");
                    System.out.println(LocalTime.now());
                    Thread.sleep(4000);
                    Gdx.app.postRunnable(new Runnable() { // from class: com.corona.game.GenerateWorld.1.1
                        @Override // java.lang.Runnable
                        public void run() {
                            genIsland.step(game);
                            EnterBuilding enterBuilding2 = new EnterBuilding(game, "", null);
                            enterBuilding2.slow = 8;
                            game.insertAction(enterBuilding2);
                            game.insertAction(new DisplayText.Clear(game, new SetField(drawControls, "remove", true, null)));
                            Vector2 startLoc2 = new Vector2();
                            if (enterDirection.equals("left")) {
                                startLoc2.x = game.map.topRight.x - 16.0f;
                                startLoc2.y = game.map.bottomLeft.y + (percent * (game.map.topRight.y - game.map.bottomLeft.y));
                            } else if (enterDirection.equals("right")) {
                                startLoc2.x = game.map.bottomLeft.x;
                                startLoc2.y = game.map.bottomLeft.y + (percent * (game.map.topRight.y - game.map.bottomLeft.y));
                            } else if (enterDirection.equals("up")) {
                                startLoc2.y = game.map.bottomLeft.y;
                                startLoc2.x = game.map.bottomLeft.x + (percent * (game.map.topRight.x - game.map.bottomLeft.x));
                            } else if (enterDirection.equals("down")) {
                                startLoc2.y = game.map.topRight.y - 16.0f;
                                startLoc2.x = game.map.bottomLeft.x + (percent * (game.map.topRight.x - game.map.bottomLeft.x));
                            }
                            startLoc2.x -= startLoc2.x % 16.0f;
                            startLoc2.y -= startLoc2.y % 16.0f;
                            game.player.position.set(startLoc2);
                            game.cam.position.set(startLoc2.x + 16.0f, startLoc2.y, 0.0f);
                            Vector2 nearestEdge = game.map.edges.get(0);
                            float minDistance = game.player.position.dst2(nearestEdge);
                            Iterator<Vector2> it5 = game.map.edges.iterator();
                            while (it5.hasNext()) {
                                Vector2 edge = it5.next();
                                float currDistance = game.player.position.dst2(edge);
                                if (minDistance > currDistance) {
                                    nearestEdge = edge;
                                    minDistance = currDistance;
                                }
                            }
                            game.player.spawnLoc = nearestEdge.cpy();
                            game.player.spawnIndex = -1;
                            game.map.minimap = new Pixmap(((int) (game.map.topRight.x - game.map.bottomLeft.x)) / 8, ((int) (game.map.topRight.y - game.map.bottomLeft.y)) / 8, Pixmap.Format.RGBA8888);
                            game.map.minimap.setColor(0.0f, 0.0f, 0.0f, 1.0f);
                            game.map.minimap.fill();
                            Vector2 startPos = game.player.position.cpy().add(-128.0f, -128.0f);
                            startPos.x = (float) (((int) startPos.x) - (((int) startPos.x) % 16));
                            startPos.y = (float) (((int) startPos.y) - (((int) startPos.y) % 16));
                            Vector2 endPos = game.player.position.cpy().add(128.0f, 128.0f);
                            endPos.x = (float) (((int) endPos.x) - (((int) endPos.x) % 16));
                            endPos.y = (float) (((int) endPos.y) - (((int) endPos.y) % 16));
                            Vector2 currPos = new Vector2(startPos.x, startPos.y);
                            while (currPos.y < endPos.y) {
                                Tile tile = game.map.tiles.get(currPos);
                                currPos.x += 16.0f;
                                if (currPos.x > endPos.x) {
                                    currPos.x = startPos.x;
                                    currPos.y += 16.0f;
                                }
                                if (tile != null) {
                                    tile.updateMiniMap(game);
                                }
                            }
                            game.insertAction(nextAction);
                            game.insertAction(new CycleDayNight(game));
                            game.musicController.startTimeOfDay = game.map.timeOfDay;
                            game.map.currMapId = adjacentWorldId;
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setPriority(1);
        thread.start();
        game.insertAction(new DisplayText(game, "Generating... please wait...", (String) null, true, false, (Action) null));
        game.insertAction(drawControls);
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
    }
}
